package com.example.node.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.node.R;
import com.example.node.base.BaseActivity;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.MovieMoreModel;
import com.example.node.utils.GlideUtils;
import com.example.node.utils.LogUtils;
import com.example.node.utils.OkHttpUtils;
import com.example.node.view.AutoViewPager;
import com.google.gson.Gson;
import java.io.IOException;

public class MovieMoreActivity extends BaseActivity implements View.OnClickListener {

    private String title;
    private String id;
    private ImageView iv_detailcover;
    private TextView tv_officialstory;
    private TextView tv_info;
    private AutoViewPager mAutoViewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_more);

        initView();
    }

    private void initView() {
        //接收数据
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        title = intent.getStringExtra("title");

        if (!TextUtils.isEmpty(title)) {
            getSupportActionBar().setTitle(title);
        }

        iv_detailcover = findViewById(R.id.iv_detailcover);
        iv_detailcover.setOnClickListener(this);
        tv_officialstory = findViewById(R.id.tv_officialstory);
        tv_info = findViewById(R.id.tv_info);

        if (!TextUtils.isEmpty(id)) {
            initData(id);
        }

        mAutoViewPager=findViewById(R.id.mAutoViewPager);
        mAutoViewPager.setAutoTime(2000);
        mAutoViewPager.setStartAuto(true);
    }

    //加载数据
    private void initData(String id) {
        String url = "http://v3.wufazhuce.com:8000/api/movie/detail/"+id;
        OkHttpUtils.get(url, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {
                LogUtils.I(e.toString());
            }

            @Override
            public void onFailure(String meg) {
                LogUtils.I(meg);
            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    loadMovieMoreData(json);
                } else {
                    LogUtils.I("数据为空");
                }
            }
        });
    }

    //解析数据
    private void loadMovieMoreData(String json) {
        MovieMoreModel movieMoreModel = new Gson().fromJson(json, MovieMoreModel.class);
        GlideUtils.loadImageView(this,movieMoreModel.getData().getDetailcover(),iv_detailcover);
        tv_officialstory.setText(movieMoreModel.getData().getOfficialstory());
        tv_info.setText(movieMoreModel.getData().getInfo());

        for (int i=0;i<movieMoreModel.getData().getPhoto().size();i++){
            LogUtils.I(movieMoreModel.getData().getPhoto().get(i));
            View view=View.inflate(this,R.layout.layout_auto_view_page_item,null);
            ImageView iv=view.findViewById(R.id.iv_bg);
            GlideUtils.loadImageView(this,movieMoreModel.getData().getPhoto().get(i),iv);
            mAutoViewPager.setView(view);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
